package com.manong.bookstore.store.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.manong.bookstore.store.entity.StoreEntity;
import com.manong.bookstore.store.entity.StoreKeeperPasswordEntity;
import com.manong.bookstore.store.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.manong.bookstore.store.entity.StorekeeperEntity;
import com.manong.bookstore.store.service.StorekeeperService;
import com.manong.common.utils.R;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27 21:38:41
 */
@RestController
@RequestMapping("store/storekeeper")
public class StorekeeperController {
    @Autowired
    private StorekeeperService storekeeperService;
    @Autowired
    private StoreService storeService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = storekeeperService.queryPage(params);
//        return R.ok().put("page", page);
//    }


    /**
     * 信息
     */
    @RequestMapping("/info/{storekeeperId}")
    public R info(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId, HttpServletResponse response, HttpServletRequest request, @PathVariable("storekeeperId") Integer storekeeperId){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId())){
                if(storekeeperId==0 && request.getSession().getAttribute("storeKeeperId")!=null && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())) {
                    storekeeperId = Integer.valueOf(request.getSession().getAttribute("storeKeeperId").toString());
                }
                StorekeeperEntity storekeeper = storekeeperService.getById(storekeeperId);
                int storeStatus=0;
                int storeId=0;
                StoreEntity store=storeService.getByStoreKeeperId(storekeeperId);
                if (store == null) {
                    storeStatus=0;
                    storeId=0;
                }else{
                    storeStatus=1;
                    storeId=store.getStoreId();
                    request.getSession().setAttribute("storeId",storeId);
                }
//                System.out.println(skId.equals(request.getSession().getAttribute("storeKeeperId").toString()));
//                System.out.println("cookies:storeKeeperId="+skId);
//                System.out.println("session:storeKeeperId="+request.getSession().getAttribute("storeKeeperId"));
                storekeeper.setPassword("");
                return R.ok().put("storekeeper", storekeeper).put("storeStatus",storeStatus).put("storeId",storeId);
            }
            return R.error("请先登录！");
        }
        return R.error("系统出错！");

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody StorekeeperEntity storekeeper){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                storekeeperService.save(storekeeper);
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody StorekeeperEntity storekeeper){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                storekeeperService.updateById(storekeeper);
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }

    /**
     * 修改密码
     */
    @RequestMapping("/updatePassword")
    public R updatePassword(@CookieValue("storeKeeperId") String skId, @CookieValue("JSESSIONID") String sessionId, HttpServletRequest request,
                            @RequestBody StoreKeeperPasswordEntity passwordEntity){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                StorekeeperEntity storekeeper=storekeeperService.getById(passwordEntity.getStoreKeeperId());
                System.out.println("updatePassword:"+storekeeper);
                System.out.println("updatePassword:"+passwordEntity);
                if(storekeeper.getPassword().equals(passwordEntity.getOldPassword())){
                    storekeeper.setPassword(passwordEntity.getNewPassword());
                    storekeeperService.updateById(storekeeper);
                    return R.ok();
                }
                return R.error("旧密码输入错误！");
            }
        }else {
            System.out.println("session==null");
        }
        return R.error("请先登录!");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@CookieValue("storeKeeperId") String skId,@CookieValue("JSESSIONID") String sessionId,HttpServletRequest request,@RequestBody Integer[] storekeeperIds){
        if (sessionId!=null){
            if(sessionId.equals(request.getSession().getId()) && skId.equals(request.getSession().getAttribute("storeKeeperId").toString())){
                storekeeperService.removeByIds(Arrays.asList(storekeeperIds));
                return R.ok();
            }
        }
        return R.error("请先登录!");
    }
    @RequestMapping("/login")
    public R login(HttpServletRequest request, HttpServletResponse response, @RequestBody StorekeeperEntity storekeeper){
        List<StorekeeperEntity> result = storekeeperService.list(new QueryWrapper<StorekeeperEntity>().eq("storekeeper_name", storekeeper.getStorekeeperName()));

        if(result.size() == 1) {
            if(result.get(0).getPassword().equals(storekeeper.getPassword())){
                Cookie cookie=new Cookie("storeKeeperId", result.get(0).getStorekeeperId().toString());
                cookie.setPath("/");
                response.addCookie(cookie);
                request.getSession().setAttribute("storeKeeperId",result.get(0).getStorekeeperId());
                return R.ok().put("status", "ok").put("storeKeeperId",result.get(0).getStorekeeperId());
            }
            else
                return R.error().put("status", "error");
        }
        else {
            return R.error().put("status", "用户名重复");
        }
    }
    @RequestMapping("/register")
    public R register(@RequestBody StorekeeperEntity storekeeperEntity){
        System.out.println(storekeeperEntity);

        Integer storekeeperId = storekeeperService.register(storekeeperEntity);

        if(storekeeperId == -1) {
            return R.ok().put("status", "error").put("message", "用户名重复");
        }
        else if(storekeeperId == -2) {
            return R.ok().put("status", "error").put("message", "手机号码重复");
        }
        else {
            return R.ok().put("status", "ok").put("storekeeperId",storekeeperId );
        }
    }

}
